package sw6.digipecs.widget.admin;

import sw6.digipecs.widget.dragdrop.DragLayout;
import sw6.digipecs.widget.dragdrop.DraggableView;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * The ImageBrowser is responsible for loading, and showing, all images available in the android MediaStore
 * The ImageBrowser is a widget that can be used in any activity consisting of a gridview showing all images in the android media store. 
 * @author sw6a
 *
 */
public class ImageStoreBrowser extends GridView {
	private DragLayout mOuterDragLayout = null; 
	
	/**
	 * This image adapter is responsible for binding the individual images from the mediastore to elements in the gridview
	 * @author sw6a
	 *
	 */
	private class ImageAdapter extends BaseAdapter {
		
		private Cursor mCursor;
		private Context mContext;
		private DragLayout mDragLayout = null;
		
		/**
		 * Standard constructor
		 * @param context Context, application context
		 */
		public ImageAdapter(Context context) {
			mContext = context;
		}
		
		/**
		 * This method fetches all images from the mediastore and stores them in a cursor
		 * @param and Activity the activity used for the managed query
		 * @param dragLayout DragLayout
		 */
		public void fetchData(Activity activity, DragLayout dragLayout) {
			this.mDragLayout=dragLayout; 
			String[] fields = {MediaStore.Images.Thumbnails.DATA };
			mCursor = activity.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, fields, MediaStore.Images.Thumbnails.KIND+"="+MediaStore.Images.Thumbnails.MINI_KIND, null, MediaStore.Images.Thumbnails.DATA);
		}
		
		/**
		 * This method return the number of elements in the cursor
		 */
		@Override
		public int getCount() {
			if(mCursor==null)
				return 0; 
			return mCursor.getCount();
		}

		/**
		 * This method returns an image in a given position
		 */
		@Override
		public Object getItem(int pos) {
			final int index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
			mCursor.moveToPosition(pos);
			return mCursor.getString(index);
		}

		/**
		 * Should return a image with a given id, but is not implemented. 
		 */
		@Override
		public long getItemId(int pos) {
			return pos;
		}
		/**
		 * Returns a view, check the android documentation :-)
		 */
		@Override
		public View getView(int pos, View convertView, ViewGroup parent) {
			ImageStoreView iv;
			
			if (convertView == null) {
				/*A ImageViewWithsurce is necessary because an element in the list could be dragged to the pecs book. 
				 *In this case the droptarget need to know the location of the image to add it to the pecs book */
				iv = new ImageStoreView(mContext);
	            iv.setLayoutParams(new GridView.LayoutParams(150, 150));
	            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
	            iv.setPadding(8, 8, 8, 8);
	            
	            if(mDragLayout!=null)
	            {
	            	iv.setOnLongClickListener(new OnLongClickListener() {
						/**
						 * A longclicklistener is used to ensure that pictures are not accidently dragged when the user wants to scroll the gridview
						 */
						@Override
						public boolean onLongClick(View v) {
							if (v instanceof DraggableView) {
								mDragLayout.startDrag((DraggableView)v);
								Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);							 
								vibrator.vibrate(150);
								
								return true;
							}

							return false;
						}
					}); 
	            }
	            
			} else {
				iv = (ImageStoreView) convertView;
			}
			
			mCursor.moveToPosition(pos); 
			String path = mCursor.getString(0); 
            iv.setUri(Uri.parse(path));
			iv.setImageURI(Uri.parse((String) getItem(pos)));	
			return iv;
		}
	}
	
	private ImageAdapter mAdapter;

	/**
	 * Image Adapter constructor
	 * @param context Context
	 * @param attrs AttributeSet
	 */
	public ImageStoreBrowser(Context context, AttributeSet attrs) {
		super(context, attrs);
		mAdapter = new ImageAdapter(context);
		setAdapter(mAdapter);
	}
	
	/**
	 * This method fetches all data
	 * @param activity Activity
	 * @param dragLayout Draglayout
	 */
	public void fetchData(Activity activity,DragLayout dragLayout) {
		mOuterDragLayout=dragLayout; 
		mAdapter.fetchData(activity,mOuterDragLayout);
	}

}
